<?php
session_start();
if (!isset($_SESSION['admin']))
    header('location:index.php');
?>

<!DOCTYPE HTML>
<html>

    <head>
        <title>3D|didongde 3D- Di động đây</title>
        <?php include('modules/page_head.php'); ?>
    </head>

    <body>
        <div id="main">
            <div id="header">
                <div id="logo">
                    <h1><a href="index.php">Didongde</a></h1>
                    <div class="slogan">Where you can find your own best suitable mobile</div>
                    <?php include('modules/logo_header.php'); ?>
                </div>
                <div id="menubar">
                    <?php include('modules/menubar.php'); ?>
                </div>
            </div>
            <div id="site_content">
                <div id="sidebar_container">
                    <h2>Tìm kiếm nhanh</h2>
                    <div class="sidebar">
                        <?php
                        include ("modules/quick_search.php");
                        show_manu();
                        ?>
                    </div>
                    <div class="sidebar">
                        <?php show_price(); ?>
                    </div>
                </div>
                <div id="content">
                    <!-- insert the page content here -->
                    <?php
                    include_once('conf/dbconfig.php');
                    $cfg = new dbconfig();
                    $host = $cfg->get_dbhost();
                    $db = $cfg->get_dbname();
                    $usr = $cfg->get_dbusername();
                    $pass = $cfg->get_dbpwd();
                    $connection = pg_connect("host = $host dbname = $db user = $usr password = $pass")
                            or die("Cannot connect to database server: ");
                    ?>
                    <h2>Xóa sản phẩm khỏi kho hàng</h2>
                    <form method="post" action="#">
                        <table>                            
                            <tr><td>Model</td><td>
                                    <?php
                                    $query = "select * from mobilephone order by model";
                                    $result = pg_query($connection, $query);
                                    echo "<select id=\"model\" name = \"model\">";
                                    echo "<option value=\"này\">Chọn sản phẩm cần xóa</option>";
                                    for ($i = 0; $i < pg_num_rows($result); $i++) {
                                        $name = pg_fetch_result($result, $i, 'model');
                                        echo "<option value = \"$name\"";
                                        if (isset($_REQUEST['model']) && $_REQUEST['model'] == $name) {
                                            echo " selected=\"selected\"";
                                        }
                                        echo ">$name</option>";
                                        echo "</option>";
                                    }
                                    echo "</select>";
                                    pg_free_result($result);
                                    ?>
                                </td></tr>
                            <tr><td colspan="2">
                                    <input style="padding:2px 4px 2px 4px;" type="submit" name="submit" value="Gửi yêu cầu"/>
                                </td></tr>
                        </table>
                    </form>

                    <div>
                        <?php
                        /* connect to database */
                        if (isset($_REQUEST['submit'])) {
                            if (isset($_POST['model']))
                                $model = $_POST['model'];
                            else
                                $model = null;
                            if ($model != null) {
                                $query = "select * from mobilephone where upper(model) = '" . strtoupper($model) . "'";
                                $result = pg_query($connection, $query);
                                if (pg_num_rows($result) == 0)
                                    echo("Model không tồn tại");
                                else {
                                    $_SESSION['mbmodel'] = $model;
                                    echo "<form method=\"post\" action=\"#\">";
                                    echo "<table>";
                                    echo "<tr><td>Model</td><td>$model</td></tr>";
                                    echo "<tr><td>Giá bán</td><td>";
                                    echo number_format(pg_fetch_result($result, 0, 'price'), 3, ',', ',') . "</td></tr>";
                                    echo "<tr><td>Số lượng còn trong kho</td><td>";
                                    echo pg_fetch_result($result, 0, 'quantity') . "</td></tr>";
                                    echo "<tr><td colspan=\"2\">";
                                    echo "<input style=\"padding:2px 4px 2px 4px;\" type=\"submit\" name=\"del\" value=\"Xóa sản phẩm này\" onclick=\"return confirm('Bạn chắc chắn muốn xóa sản phẩm này?');\"/>";
                                    echo "</table>";
                                }
                            }
                        }
                        if (isset($_REQUEST['del'])) {
                            $model = $_SESSION['mbmodel'];
                            $query = "select * from mobilephone";
                            $result = pg_query($connection, $query);
                            $row = pg_num_rows($result);
                            $query = "select * from mobilephone where model = '" . $model . "'";
                            $result = pg_query($connection, $query);
                            $id = pg_fetch_result($result, 0, 'mobileid');
                            $query = "delete from mobilephone where model ='" . $model . "'";
                            $result = pg_query($connection, $query);
                            if (pg_affected_rows($result)) {
                                for ($i = $id + 1; $i <= $row; $i++) {
                                    $oldid = strval($i);
                                    while (strlen($oldid) < 4)
                                        $oldid = "0" . $oldid;
                                    $new_id = strval($i - 1);
                                    while (strlen($new_id) < 4)
                                        $new_id = "0" . $new_id;
                                    $query = "update mobilephone set mobileid = '$new_id' where mobileid = '$oldid'";
                                    $result = pg_query($connection, $query);
                                }
                                echo "Xóa thành công thành công model " . $model;
                            }
                            pg_free_result($result);
                        }
                        pg_close($connection);
                        ?>
                    </div>
                </div>
            </div>
        </div>
        <div id="footer">
            <?php include('modules/footer.php'); ?>
        </div>
    </body>
</html>
